package com.es.servlet;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * 描述:
 * es-servlet-
 *
 * @author Derek
 * 创建时间：2022-04-21 11:54
 */
@WebServlet(urlPatterns = "/readCookie")
public class ReadCookie extends HttpServlet {
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //获取客户端发送过来的cookie
        Cookie[] cookies = req.getCookies();
        String username = null;
        String password = null;
        if(cookies != null){
            for (int i = 0; i < cookies.length; i++) {
                Cookie cookie = cookies[i];
                if(cookie.getName().equals("username")){
                    username = cookie.getValue();
                }else{
                    if(cookie.getName().equals("password")){
                        password=cookie.getValue();
                    }
                }
            }
        }
        //
        resp.setContentType("text/html;charset=utf-8");
        PrintWriter out = resp.getWriter();
        out.println("读取到的username是："+username);
        out.println("<br>读取到的password是："+password);
        out.flush();
    }
}
